@model List<CaipuGuanliXitong.Models.WeeklyMenu>

@{
    ViewData["Title"] = "每周菜谱详情";
}

<h2>每周菜谱详情</h2>

<div class="row">
    <div class="col-md-12">
        <form method="get" action="@Url.Action("Index")" class="form-inline">
            <div class="form-group">
                <label for="startDate">选择周开始日期:</label>
                <input type="date" name="startDate" value="@ViewBag.StartDate.ToString("yyyy-MM-dd")" class="form-control" />
            </div>
            <button type="submit" class="btn btn-primary">查询</button>
            <a href="@Url.Action("Index")" class="btn btn-secondary">返回列表</a>
        </form>
    </div>
</div>

<table class="table table-bordered">
    <thead>
        <tr>
            <th>星期</th>
            <th colspan="2">早餐</th>
            <th colspan="2">午餐</th>
            <th colspan="2">晚餐</th>
        </tr>
        <tr>
            <th></th>
            <th>男</th>
            <th>女</th>
            <th>男</th>
            <th>女</th>
            <th>男</th>
            <th>女</th>
        </tr>
    </thead>
    <tbody>
        @{
            string[] weekdays = { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
            int[] dayNumbers = { 1, 2, 3, 4, 5, 6, 7 };
            int[] mealNumbers = { 1, 2, 3 }; // 1=早餐, 2=午餐, 3=晚餐
            int[] genders = { 1, 2 }; // 1=男, 2=女
        }
        
        @for (int i = 0; i < weekdays.Length; i++)
        {
            <tr>
                <td>@weekdays[i]</td>
                @for (int j = 0; j < mealNumbers.Length; j++)
                {
                    @for (int k = 0; k < genders.Length; k++)
                    {
                        <td>
                            @{
                                var menu = Model.FirstOrDefault(m => 
                                    m.DayOfWeek == dayNumbers[i] && 
                                    m.MealType == mealNumbers[j] && 
                                    m.Gender == genders[k]);
                                    
                                if (menu != null)
                                {
                                    if (!string.IsNullOrEmpty(menu.CookBookNames))
                                    {
                                        // 显示多个菜谱名称
                                        var names = menu.CookBookNames.Split(',');
                                        <ul class="list-unstyled mb-0">
                                            @foreach (var name in names)
                                            {
                                                @if (!string.IsNullOrEmpty(name))
                                                {
                                                    <li>@name</li>
                                                }
                                            }
                                        </ul>
                                    }
                                    else if (!string.IsNullOrEmpty(menu.CookBookName))
                                    {
                                        // 显示单个菜谱名称（向后兼容）
                                        @menu.CookBookName
                                    }
                                }
                            }
                        </td>
                    }
                }
            </tr>
        }
    </tbody>
</table>

<div class="form-group">
    <a href="@Url.Action("Edit", new { startDate = ViewBag.StartDate })" class="btn btn-primary">编辑</a>
    <a href="@Url.Action("Delete", new { startDate = ViewBag.StartDate })" class="btn btn-danger">删除</a>
    <a href="@Url.Action("Index")" class="btn btn-secondary">返回列表</a>
</div>